package cn.iocoder.yudao.module.exam.service.paperquestionbank;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.paperquestionbank.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.paperquestionbank.PaperQuestionBankDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.paperquestionbank.PaperQuestionBankMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.exam.enums.ErrorCodeConstants.*;

/**
 * 试卷题库关联 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PaperQuestionBankServiceImpl implements PaperQuestionBankService {

    @Resource
    private PaperQuestionBankMapper paperQuestionBankMapper;

    @Override
    public Long createPaperQuestionBank(PaperQuestionBankSaveReqVO createReqVO) {
        // 插入
        PaperQuestionBankDO paperQuestionBank = BeanUtils.toBean(createReqVO, PaperQuestionBankDO.class);
        paperQuestionBankMapper.insert(paperQuestionBank);
        // 返回
        return paperQuestionBank.getId();
    }

    @Override
    public void updatePaperQuestionBank(PaperQuestionBankSaveReqVO updateReqVO) {
        // 校验存在
        validatePaperQuestionBankExists(updateReqVO.getId());
        // 更新
        PaperQuestionBankDO updateObj = BeanUtils.toBean(updateReqVO, PaperQuestionBankDO.class);
        paperQuestionBankMapper.updateById(updateObj);
    }

    @Override
    public void deletePaperQuestionBank(Long id) {
        // 校验存在
        validatePaperQuestionBankExists(id);
        // 删除
        paperQuestionBankMapper.deleteById(id);
    }

    private void validatePaperQuestionBankExists(Long id) {
        if (paperQuestionBankMapper.selectById(id) == null) {
            throw exception(PAPER_QUESTION_BANK_NOT_EXISTS);
        }
    }

    @Override
    public PaperQuestionBankDO getPaperQuestionBank(Long id) {
        return paperQuestionBankMapper.selectById(id);
    }

    @Override
    public PageResult<PaperQuestionBankDO> getPaperQuestionBankPage(PaperQuestionBankPageReqVO pageReqVO) {
        return paperQuestionBankMapper.selectPage(pageReqVO);
    }

}